package utils;

import pojo.Admin;

import java.util.List;
import java.util.Random;

public class RegisterUtil {
    /**
     * 获取验证码
     *
     * @return
     */
    public static String getCAPTCHA() {
        Random random = new Random();
        //长度为5
        char[] CAPTCHA = new char[5], LetterVector = getLetterVector();
        //由4位大写或者小写字母和1位数字组成
        //数字可以出现在任意位置
        int number = random.nextInt(10), numberIndex = random.nextInt(CAPTCHA.length);
        CAPTCHA[numberIndex] = (char) (number + '0');
        int LetterLength = LetterVector.length;
        for (int i = 0; i < CAPTCHA.length; i++) {
            if (i != numberIndex) {
                CAPTCHA[i] = LetterVector[random.nextInt(LetterLength)];
            }
        }
        return new String(CAPTCHA);
    }

    /**
     * 获取用户名在集合中的的索引
     * @param list
     * @param username
     * @return
     */
    public static int getIndex(List<Admin> list, String username){
        if(list == null || list.size() == 0){
            return -1;
        }
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getUsername().equals(username)){
                return i;
            }
        }
        return -1;
    }

    private static char[] getLetterVector() {
        char[] letterVector = new char[52];
        for (int i = 0; i < 26; i++) {
            letterVector[i] = (char) (i + 97);
            letterVector[i + 26] = (char) (i + 65);
        }
        return letterVector;
    }
}
